package com.qf.adminController;

import com.alibaba.fastjson.JSON;
import com.qf.adminService.AdminUserService;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.adminController
 * @Description:
 * @Date 2022/10/4 16:30
 */
@RestController
@RequestMapping("/adminuser")
public class AdminUserController {

    @Autowired
    AdminUserService adminUserService;

    @PostMapping("/adminLogin")
    public ModelAndView adminLogin(String username, String password,
                           HttpSession session,
                           ModelAndView mav){
        User user = adminUserService.findUseByNameAndPwd(username, password);
        if (user==null || user.getRole()!=0){
            mav.setViewName("redirect:/admin/login.jsp");
            return mav;
        }else {
            session.setAttribute("admin",user);
            mav.setViewName("redirect:/admin/admin.jsp");
            return mav;
        }
    }


    @GetMapping("/getUserList")
    public String getUserList(HttpSession session){
        User user = (User) session.getAttribute("admin");
        List<User> userList = adminUserService.selectAllUser();
        String string = JSON.toJSONString(userList);
        return string;
    }

    @GetMapping("/deleteUser")
    public void deleteUser(Integer id,HttpSession session){

        adminUserService.deleteUser(id);
    }

    @PostMapping("/searchUser")
    private String searchUser(String username,String gender){
        List<User> userList = adminUserService.findUseByNameAndGender(username, gender);
        String string = JSON.toJSONString(userList);
        return string;
    }

    @GetMapping("/getInvalidList")
    public String getInvalidList(){
        List<User> invalidUser = adminUserService.findInvalidUser();
        String string = JSON.toJSONString(invalidUser);
        return string;
    }
}
